package com.ruoyi.loan.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.loan.domain.TbRefund;
import com.ruoyi.loan.mapper.TbLoanMapper;
import com.ruoyi.loan.domain.TbLoan;
import com.ruoyi.loan.service.ITbLoanService;

/**
 * 贷款申请Service业务层处理
 * 
 * @author jy
 * @date 2023-01-16
 */
@Service
public class TbLoanServiceImpl implements ITbLoanService 
{
    @Autowired
    private TbLoanMapper tbLoanMapper;

    /**
     * 查询贷款申请
     * 
     * @param loanId 贷款申请主键
     * @return 贷款申请
     */
    @Override
    public TbLoan selectTbLoanByLoanId(Long loanId)
    {
        return tbLoanMapper.selectTbLoanByLoanId(loanId);
    }

    /**
     * 查询贷款申请列表
     * 
     * @param tbLoan 贷款申请
     * @return 贷款申请
     */
    @Override
    public List<TbLoan> selectTbLoanList(TbLoan tbLoan)
    {
        return tbLoanMapper.selectTbLoanList(tbLoan);
    }

    /**
     * 新增贷款申请
     * 
     * @param tbLoan 贷款申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbLoan(TbLoan tbLoan)
    {
        tbLoan.setCreateTime(DateUtils.getNowDate());
        int rows = tbLoanMapper.insertTbLoan(tbLoan);
        insertTbRefund(tbLoan);
        return rows;
    }

    /**
     * 修改贷款申请
     * 
     * @param tbLoan 贷款申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbLoan(TbLoan tbLoan)
    {
        tbLoan.setUpdateTime(DateUtils.getNowDate());
        tbLoanMapper.deleteTbRefundByRefundId(tbLoan.getLoanId());
        insertTbRefund(tbLoan);
        return tbLoanMapper.updateTbLoan(tbLoan);
    }

    /**
     * 批量删除贷款申请
     * 
     * @param loanIds 需要删除的贷款申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbLoanByLoanIds(Long[] loanIds)
    {
        tbLoanMapper.deleteTbRefundByRefundIds(loanIds);
        return tbLoanMapper.deleteTbLoanByLoanIds(loanIds);
    }

    /**
     * 删除贷款申请信息
     * 
     * @param loanId 贷款申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbLoanByLoanId(Long loanId)
    {
        tbLoanMapper.deleteTbRefundByRefundId(loanId);
        return tbLoanMapper.deleteTbLoanByLoanId(loanId);
    }

    /**
     * 新增还款方式信息
     *
     * @param tbLoan 贷款申请对象
     */
    public void insertTbRefund(TbLoan tbLoan)
    {
        List<TbRefund> tbRefundList = tbLoan.getTbRefundList();
        Long loanId = tbLoan.getLoanId();
        if (StringUtils.isNotNull(tbRefundList))
        {
            List<TbRefund> list = new ArrayList<TbRefund>();
            for (TbRefund tbRefund : tbRefundList)
            {
                tbRefund.setRefundId(loanId);
                list.add(tbRefund);
            }
            if (list.size() > 0)
            {
                tbLoanMapper.batchTbRefund(list);
            }
        }
    }
}
